﻿using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace FPSO.HMI.Controls
{
    /// <summary>
    /// UCDateTime.xaml 的交互逻辑
    /// </summary>
    public partial class UCDateTime : UserControl
    {
        public UCDateTime()
        {
            InitializeComponent();
        }

        public static readonly DependencyProperty ValueProperty =
            DependencyProperty.Register("Value", typeof(DateTime), typeof(UCDateTime), new PropertyMetadata(DateTime.Now));
        public DateTime Value
        {
            get { return (DateTime)GetValue(ValueProperty); }
            set { SetValue(ValueProperty, value); }
        }

        [Category("Action")]
        [Description("选择的时间被修改")]
        public event Action<DateTime> OnDateTimeChanged;

        protected override void OnRender(DrawingContext drawingContext)
        {
            base.OnRender(drawingContext);
            //ControlUpdate();
        }

        private void btnDate_Click(object sender, RoutedEventArgs e)
        {
            if (popChioce.IsOpen)
            {
                popChioce.IsOpen = false;
            }

            UCDateView dateView = new UCDateView();
            dateView.DateClick += (date) =>
            {
                if (!string.IsNullOrEmpty(date))
                {
                    string newTimeStr = string.Format("{0} {1}", date, Value.ToString("HH:mm:ss"));
                    Value = Convert.ToDateTime(newTimeStr);
                    //ControlUpdate();
                }
                popChioce.IsOpen = false;
                OnDateTimeChanged?.Invoke(Value);
            };
            popChioce.Child = dateView;
            popChioce.Width = 140;
            popChioce.Height = 140;
            popChioce.IsOpen = true;
        }

        //private void ControlUpdate()
        //{
        //    //btnDate.Content = Value.ToString("yyyy-MM-dd");
        //    //btnTime.Content = Value.ToString("HH:mm:ss");
        //}

        private void btnTime_Click(object sender, RoutedEventArgs e)
        {
            if (popChioce.IsOpen)
            {
                popChioce.IsOpen = false;
            }

			int hour = Value.Hour;
			int minute = Value.Minute + (5 - Value.Minute % 5);
			if(minute >= 60)
			{
				hour += 1;
				minute -= 1;
				if (hour >= 24)
					hour = 0;
			}

			UCTimeView timeView = new UCTimeView(hour, minute);
            timeView.TimeClick += (h, m) =>
            {
                if (h >= 0 && m >= 0)
                {
                    string newTimeStr = string.Format("{0} {1}:{2}:0", Value.ToString("yyyy-MM-dd"), h, m);
                    Value = Convert.ToDateTime(newTimeStr);
                    //ControlUpdate();
                }
                popChioce.IsOpen = false;
                OnDateTimeChanged?.Invoke(Value);
            };
            popChioce.Child = timeView;
            popChioce.Width = 390;
            popChioce.Height = 222;
            popChioce.IsOpen = true;
        }
    }
}
